$(function () {
    $("#jqGrid").Grid({
        url: '../lotterywinning/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '红球', name: 'redBall', index: 'red_ball', width: 80},
			{label: '蓝球', name: 'blueBall', index: 'blue_ball', width: 80},
			{label: '期号', name: 'stages', index: 'stages', width: 80},
            {label: '开奖状态', name: 'status', index: 'status', width: 80, formatter: function (value) {
                    if(value==0)
                        return '未开奖';
                    else if(value==1)
                        return '已开奖';
                    else
                        return '';
                }},
            {label: '开奖文案', name: 'text', index: 'openDate', width: 80},
            {label: '开奖时间', name: 'openDate', index: 'openDate', width: 80, formatter: function (value) {
                    return transDate(value);
                }},
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80, formatter: function (value) {
                    return transDate(value);
                }},
            {name:'已经存在的列名称', label:'操作', width: 130,formatter:function(cellvalue, options, row){
                    if(row.status==0)
                    	return '<a href="#" onclick="vm.openSpe(\''+row.id+'\')">开奖</a>';
                    else
                        return '';
                }
            }]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		lotteryWinning: {htb:'',hongtb:'',mhb:'',fkb:''},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.lotteryWinning = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
        openSpe: function (id) {

            if (id == null) {
                return;
            }
            vm.getInfo(id);
            vm.lotteryWinning.htb = '';
            vm.lotteryWinning.hongtb = '';
            vm.lotteryWinning.mhb = '';
            vm.lotteryWinning.fkb = '';
            vm.title = "开奖";
            vm.showList = false;
        },
        openlotter: function (event) {
            //let url = vm.lotteryWinning.id == null ? "../lotterywinning/save" : "../lotterywinning/update";
            var htb = ''
            if(vm.lotteryWinning.htb.length>1)
                htb = '1'+vm.lotteryWinning.htb;
            else
                htb = '10'+vm.lotteryWinning.htb;

            var hongtb = ''
            if(vm.lotteryWinning.hongtb.length>1)
                hongtb = '2'+vm.lotteryWinning.hongtb;
            else
                hongtb = '20'+vm.lotteryWinning.hongtb;

            var mhb = ''
            if(vm.lotteryWinning.mhb.length>1)
                mhb = '3'+vm.lotteryWinning.mhb;
            else
                mhb = '30'+vm.lotteryWinning.mhb;

            var fkb = ''
            if(vm.lotteryWinning.fkb.length>1)
                fkb = '4'+vm.lotteryWinning.fkb;
            else
                fkb = '40'+vm.lotteryWinning.fkb;
            vm.lotteryWinning.redBall = htb+'-'+hongtb+'-'+mhb+'-'+fkb ;
            Ajax.request({
                url: "../lotterywinning/openlotter",
                params: JSON.stringify(vm.lotteryWinning),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
		saveOrUpdate: function (event) {
            let url = vm.lotteryWinning.id == null ? "../lotterywinning/save" : "../lotterywinning/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.lotteryWinning),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../lotterywinning/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../lotterywinning/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.lotteryWinning = r.lotteryWinning;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});